﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace cmsservice.Common
{
    /// <summary>
    /// Author:		 喻旻
    /// Create date: 2011-05-30
    /// Description:发送邮件
    /// </summary>
    public class MailTools
    {
        /// <summary>
        /// Smtp服务器地址
        /// </summary>
        private string SmtpServer
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["SmtpServer"];
            }
        }

        /// <summary>
        /// 域
        /// </summary>
        private string Domain
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["Domain"];
            }
        }

        /// <summary>
        /// 发件人用户名
        /// </summary>
        private string SmtpUserName
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["SmtpUserName"];
            }
        }

        /// <summary>
        /// 发件人密码
        /// </summary>
        private string PassWord
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["PassWord"];
            }
        }

        /// <summary>
        /// 发送失效时间
        /// </summary>
        private int SmtpTimeout
        {
            get
            {
                return int.Parse(System.Configuration.ConfigurationManager.AppSettings["SmtpTimeout"]);
            }
        }

        /// <summary>
        ///  Smtp端口
        /// </summary>
        private int SmtpPort
        {
            get
            {
                return int.Parse(System.Configuration.ConfigurationManager.AppSettings["SmtpPort"]);
            }
        }

        /// <summary>
        /// 邮件显示用户名
        /// </summary>
        private string Displayname
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["Displayname"];
            }
        }

        /// <summary>
        /// 对方邮件显示从哪来的邮件
        /// </summary>
        private string MailFrom
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["MailFrom"];
            }
        }

        public void SendMail(string email, string title, string content)
        {
            MailMessage mInstance = new MailMessage();
            SmtpClient smtpclent = new SmtpClient();
            mInstance.SubjectEncoding = System.Text.Encoding.UTF8;
            mInstance.Subject = title;
            mInstance.To.Add(new MailAddress(email));
            mInstance.Body = content;
            mInstance.IsBodyHtml = true;
            mInstance.BodyEncoding = System.Text.Encoding.Default;
            mInstance.Priority = MailPriority.Normal;
            smtpclent = new SmtpClient(SmtpServer, SmtpPort);
            smtpclent.Timeout = SmtpTimeout;
            smtpclent.DeliveryMethod = SmtpDeliveryMethod.Network;
            mInstance.From = new MailAddress(MailFrom, Displayname, System.Text.Encoding.UTF8);
            smtpclent.Credentials = new NetworkCredential(SmtpUserName, PassWord);
            smtpclent.Send(mInstance);
            smtpclent.Dispose();
        }

        public void SendMail(string email, string title, string content, int port, string server, string userName, string password)
        {

            MailMessage mInstance = new MailMessage();
            SmtpClient smtpclent = new SmtpClient();
            mInstance.SubjectEncoding = System.Text.Encoding.UTF8;
            mInstance.Subject = title;
            mInstance.To.Add(new MailAddress(email));
            mInstance.Body = content;
            mInstance.IsBodyHtml = true;
            mInstance.BodyEncoding = System.Text.Encoding.Default;
            mInstance.Priority = MailPriority.Normal;
            smtpclent = new SmtpClient(server, port);
            smtpclent.Timeout = SmtpTimeout;
            smtpclent.DeliveryMethod = SmtpDeliveryMethod.Network;
            mInstance.From = new MailAddress(userName, userName, System.Text.Encoding.UTF8);
            smtpclent.Credentials = new NetworkCredential(userName, password);
            smtpclent.Send(mInstance);
            smtpclent.Dispose();

        }

    }
}
